/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PGPKeyGeneratorFrame.java
 * Contains code from ppgp. See http://ppgp.sf.net
 * Created on 08.09.2008, 00:05:58
 */

package jcrypter.pgp;

/**
 *
 * @author uli
 */
public class PGPKeyGeneratorFrame extends javax.swing.JFrame {

    /** Creates new form PGPKeyGeneratorFrame */
    public PGPKeyGeneratorFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        generateKeyPairLabel = new javax.swing.JLabel();
        resultingKeyLabelLabel = new javax.swing.JLabel();
        commentLabel = new javax.swing.JLabel();
        emailLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        keySizeLabel = new javax.swing.JLabel();
        resultingKeyLabel = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        commentField = new javax.swing.JTextField();
        emailField = new javax.swing.JTextField();
        nameField = new javax.swing.JTextField();
        keySizeComboBox = new javax.swing.JComboBox();
        passphraseLabel = new javax.swing.JLabel();
        passphraseAgainLabel = new javax.swing.JLabel();
        passphraseField = new javax.swing.JPasswordField();
        passphraseAgainField = new javax.swing.JPasswordField();
        generateButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        generateKeyPairLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        generateKeyPairLabel.setText("Generate a new key pair:");

        resultingKeyLabelLabel.setText("Resulting key:");

        commentLabel.setText("Comment:");

        emailLabel.setText("E-mail:");

        nameLabel.setText("Name:");

        keySizeLabel.setText("Key size:");

        resultingKeyLabel.setText("<>");

        jLabel7.setText("bits");

        commentField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                commentFieldFocusLost(evt);
            }
        });
        commentField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                commentFieldKeyTyped(evt);
            }
        });

        keySizeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1024", "2048", "4096", "8192" }));

        passphraseLabel.setText("Passphrase:");

        passphraseAgainLabel.setText("Again:");

        generateButton.setText("Generate");
        generateButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                generateButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(generateKeyPairLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(resultingKeyLabelLabel)
                            .addComponent(commentLabel)
                            .addComponent(emailLabel)
                            .addComponent(nameLabel)
                            .addComponent(keySizeLabel)
                            .addComponent(passphraseLabel)
                            .addComponent(passphraseAgainLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(resultingKeyLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(keySizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel7))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(commentField, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(nameField, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(emailField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(passphraseField, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                            .addComponent(passphraseAgainField, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                            .addComponent(generateButton, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(generateKeyPairLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(resultingKeyLabelLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nameLabel)
                            .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(emailLabel)
                            .addComponent(emailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(commentLabel)
                            .addComponent(commentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(resultingKeyLabel)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(keySizeLabel)
                    .addComponent(jLabel7)
                    .addComponent(keySizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passphraseLabel)
                    .addComponent(passphraseField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passphraseAgainLabel)
                    .addComponent(passphraseAgainField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(generateButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void commentFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_commentFieldFocusLost
        updateKeyId();
}//GEN-LAST:event_commentFieldFocusLost

    private void commentFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_commentFieldKeyTyped
        updateKeyId();
}//GEN-LAST:event_commentFieldKeyTyped

    void updateKeyId()    
    {
        keyIdBuilder.append(nameField.getText());
        if(commentField.getText().length() > 0 )
            keyIdBuilder.append(" ("+commentField.getText()+")");
        keyIdBuilder.append(" <" + emailField.getText() + ">");
        resultingKeyLabel.setText(keyIdBuilder.toString());
        keyIdBuilder = new StringBuilder();
    }
    
    private void generateButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_generateButtonMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_generateButtonMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PGPKeyGeneratorFrame().setVisible(true);
            }
        });
    }

    StringBuilder keyIdBuilder = new StringBuilder();

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField commentField;
    private javax.swing.JLabel commentLabel;
    private javax.swing.JTextField emailField;
    private javax.swing.JLabel emailLabel;
    private javax.swing.JButton generateButton;
    private javax.swing.JLabel generateKeyPairLabel;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JComboBox keySizeComboBox;
    private javax.swing.JLabel keySizeLabel;
    private javax.swing.JTextField nameField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JPasswordField passphraseAgainField;
    private javax.swing.JLabel passphraseAgainLabel;
    private javax.swing.JPasswordField passphraseField;
    private javax.swing.JLabel passphraseLabel;
    private javax.swing.JLabel resultingKeyLabel;
    private javax.swing.JLabel resultingKeyLabelLabel;
    // End of variables declaration//GEN-END:variables

}
